package com.xps.charminguser.view.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.custom.supereasydialog.view.EasyDialog;
import com.custom.supereasydialog.view.IEasyDialog;
import com.jude.utils.JUtils;
import com.xps.charminguser.R;
import com.xps.charminguser.bean.SeeOrderBean;
import com.xps.charminguser.bean.VerificationBean;
import com.xps.charminguser.bean.WaitOrderBean;
import com.xps.charminguser.date.net.UserNetWorks;
import com.xps.charminguser.utils.AppCommond;
import com.xps.charminguser.utils.SharedPreferencesUtils;
import com.xps.charminguser.view.base.BaseActivity;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by MI on 2018/2/3.
 */

public class AcceptOrderActivity extends BaseActivity {
    @BindView(R.id.Layout_Order)
    LinearLayout LayoutOrder;
    String order_id;
    Intent intent;
    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.actionbar_tv_title)
    TextView actionbarTvTitle;

    TextView TextShopName;
    TextView TextShopAddress;
    TextView TextShopContent;
    TextView TextShopRemarks;
    TextView TextState;
    ImageView Image_Telephone;


    WaitOrderBean.ReturnBodyBean dataBean;
    @BindView(R.id.Text_Time)
    TextView TextTime;
    @BindView(R.id.Text_Payment)
    TextView TextPayment;
    @BindView(R.id.Text_Money)
    TextView TextMoney;
    @BindView(R.id.Text_Distribution)
    TextView TextDistribution;
    @BindView(R.id.Pay_by_card)
    TextView PayByCard;
    @BindView(R.id.cash)
    TextView cash;
    TextView TextDate;
    String paymentStr;
    @BindView(R.id.Pay_back)
    TextView PayBack;

    @Override
    protected int getLayout() {
        return R.layout.activity_acceptorder;
    }

    @Override
    protected void setData() {
        actionbarTvTitle.setText("订单详情");
        actionbarTvTitle.setTextColor(getResources().getColor(R.color.white));
    }

    @Override
    protected void init(Bundle inBundle) {
        ButterKnife.bind(this);
        intent = getIntent();
        showDialog();
        dataBean = (WaitOrderBean.ReturnBodyBean) intent.getSerializableExtra("dataBean");
        getSeeOrder();
        TextMoney.setText(dataBean.getOrder_money());
        TextDistribution.setText(dataBean.getOrder_fare());
        parseTime(dataBean.getOrder_create_time(), TextTime);
        if (dataBean.getOrder_payment_method().equals("0")) {
            TextPayment.setText("现金");
        } else if (dataBean.getOrder_payment_method().equals("1")) {
            TextPayment.setText("刷卡");
        }
        if (dataBean.getOrder_status().equals("3") || dataBean.getOrder_status().equals("4")) {
            PayByCard.setVisibility(View.GONE);
            cash.setVisibility(View.GONE);
        }


    }

    void parseTime(String createTime, TextView tvOrderTime) {
        long lTime = Long.valueOf(createTime);
        long nTime = lTime * 1000;
        Date passDate = new Date(nTime);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        tvOrderTime.setText(simpleDateFormat.format(passDate));
    }

    public void getSeeOrder() {
        String user_token = SharedPreferencesUtils.getString("user_token", "String", null);
        UserNetWorks.getSeeOrder(user_token, dataBean.getOrder_id(),
                new Subscriber<SeeOrderBean>() {

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        JUtils.Toast("加载失败");
                        e.printStackTrace();
                        dissmisDialog();
                    }

                    @Override
                    public void onNext(SeeOrderBean seeOrderBean) {
                        dissmisDialog();
                        if (seeOrderBean.getReturn_code().equals("SUCCESS")) {
                            LayoutOrder.removeAllViews();
                            List<SeeOrderBean.ReturnBodyBean> list = seeOrderBean.getReturn_body();
                            if (list != null && list.size() > 0) {
                                for (int i = 0; i < list.size(); i++) {
                                    addOrderItem(list.get(i), LayoutOrder);
                                    addDividerLine(LayoutOrder);
                                }
                            }
                        }
                    }
                });
    }

    public void getCompleteOrder() {
        String user_token = SharedPreferencesUtils.getString("user_token", "String", null);
        UserNetWorks.getAcceptOrder(user_token, dataBean.getOrder_id(), new Subscriber<VerificationBean>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                JUtils.Toast("接单失败");
                dissmisDialog();
            }

            @Override
            public void onNext(VerificationBean verificationBean) {
                dissmisDialog();
                if (verificationBean.getReturn_code().equals("SUCCESS")) {
                    JUtils.Toast(verificationBean.getReturn_msg());
                    AppCommond appCommond = new AppCommond.Builder()
                            .setOpration(AppCommond.CLOSE_ACTIVITY)
                            .setRequestcloseAcname(RegisterActivity.class.getSimpleName())
                            .build();
                    EventBus.getDefault().post(appCommond);
                    dissmisDialog();
                    finish();
                } else {
                    JUtils.Toast(verificationBean.getReturn_msg());
                }
            }
        });
    }

    public void addOrderItem(final SeeOrderBean.ReturnBodyBean dataBean, LinearLayout ll_lv) {
        View view = LayoutInflater.from(AcceptOrderActivity.this).inflate(R.layout.adapte_seeorder, null);
        TextShopName = view.findViewById(R.id.Text_ShopName);
        TextShopAddress = view.findViewById(R.id.Text_ShopAddress);
        TextShopContent = view.findViewById(R.id.Text_ShopContent);
        TextShopRemarks = view.findViewById(R.id.Text_ShopRemarks);
        TextDate = view.findViewById(R.id.Text_Date);
        TextState = view.findViewById(R.id.Text_State);
        Image_Telephone = view.findViewById(R.id.Image_Telephone);
        TextState.setVisibility(View.GONE);
        TextShopName.setText(dataBean.getShop_info().getShop_name());
        TextShopAddress.setText(dataBean.getShop_info().getShop_address());
        String[] Imagestr = new String[0];
        List<String> ImageArray = new ArrayList<String>();
        ImageArray.addAll(Arrays.asList(Imagestr));
        Image_Telephone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                diallPhone(dataBean.getShop_info().getShop_tel());
            }
        });
        String[] str = new String[0];
        List<String> strArray = new ArrayList<String>();
        strArray.addAll(Arrays.asList(str));
        if (dataBean != null && dataBean.getDish_info().size() > 0) {
            for (int i = 0; i < dataBean.getDish_info().size(); i++) {
                TextDate.setText(dataBean.getDish_info().get(i).getGoods_ready_time());
                ImageArray.add(String.valueOf(dataBean.getDish_info().get(i).getDish_name()) + String.valueOf(dataBean.getDish_info().get(i).getGoods_num()) + "份");
                strArray.add(String.valueOf(dataBean.getDish_info().get(i).getGoods_remarks()));
            }
        }
        TextShopRemarks.setText((strArray + "").replace("[", "").replace("]", ""));
        TextShopContent.setText((ImageArray + "").replace("[", "").replace("]", ""));
        ll_lv.addView(view);
    }

    public void diallPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }

    public void addDividerLine(LinearLayout ll_lv) {
        View v = new View(AcceptOrderActivity.this);
        v.setAlpha(0.5f);
        v.setBackgroundColor(AcceptOrderActivity.this.getResources().getColor(R.color.dialogplus_card_shadow));
        v.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        ll_lv.addView(v);
    }


    @OnClick({R.id.actionbar_iv_back, R.id.Pay_by_card, R.id.cash, R.id.Pay_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.actionbar_iv_back:
                finish();
                break;
            case R.id.Pay_by_card:
                showDialog1();
                break;
            case R.id.cash:
                showDialog1();
                break;
            case R.id.Pay_back:
                finish();
                break;
        }
    }

    public void showDialog1() {
        final EasyDialog dialog = new EasyDialog(EasyDialog.DEFAULT_TWO_BTN, AcceptOrderActivity.this);
        dialog.show();
        //设置标题
        dialog.setTitle("温馨提示");
        //设置内容
        dialog.setContent("是否接受此订单");
        //设置左侧按钮名字
        dialog.setLeftBtnName("确定");
        //设置右侧按钮名字
        dialog.setRightBtnName("取消");

        dialog.setDefaultClickTwo(new IEasyDialog.IBtnClickTwo() {
            @Override
            public void leftOnClick() {
                dialog.dismiss();
                showDialog();
                getCompleteOrder();
            }

            @Override
            public void rightOnClick() {
                dialog.dismiss();
            }
        });
    }
}
